package com.ye.utils;

import com.ye.domain.User;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.UUID;

public class Md5Util {
    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }


    public static String createSalt() {
//        UUID生成盐
        UUID salt = UUID.randomUUID();
        return salt+"";
    }

//    密码加密保存
    public static User md5Password(User user){
//        创建随机盐
        String salt = createSalt();
//        调用User.setter存储salt
        user.setSalt(salt);
//        调用User。setter存储加密后的密码
        String password = user.getPassword();
        String ps= password+salt;
        String dbpass = md5(ps);
        user.setPassword(dbpass);
        return user;
    }

//    密码解析
    public static String dmd5Password(String password,String salt){
        String dbpass = md5(password + salt);
        return dbpass;
    }

}
